package com.AAL.Action;

import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.AAL.DAO.TypeOfAutoDAO;
import com.AAL.Model.TypeOfAuto;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

@SuppressWarnings("serial")
public class TypeOfAutosAction extends ActionSupport implements SessionAware {

	private Map<String, Object> session;
	private List<TypeOfAuto> listTypeOfAuto;
	private TypeOfAutoDAO linkTypeOfAuto;
	private TypeOfAuto typeOfAuto = new TypeOfAuto();

	public TypeOfAutosAction() {
		this.linkTypeOfAuto = new TypeOfAutoDAO();
	}

	public String preAddNewTypeOfAutos() {
		return SUCCESS;
	}
	public String addNewTypeOfAutos() {
		try {

			if (null != typeOfAuto) {
				System.out.println("Ten:" + typeOfAuto.getTypeOfAutoName());
				TypeOfAuto ntyOfAuto = typeOfAuto;
				linkTypeOfAuto.saveAndUpdateTypeOfAutos(ntyOfAuto);

			}

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String getTypeOfAutos() {

		try {
			if (null != typeOfAuto) {
				System.out.println("goi ham add");
				HttpServletRequest request = (HttpServletRequest) ActionContext
						.getContext().get(ServletActionContext.HTTP_REQUEST);
				String idTypeOfAuto = request.getParameter("id");
				System.out.println("ID:" + idTypeOfAuto);
				this.typeOfAuto = this.linkTypeOfAuto
						.getTypeOfAuto(idTypeOfAuto);
				System.out.println(typeOfAuto);
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String getLstTypeOfAutos() {
		try {
			// System.out.println("Lay danh sach lop xe");
			this.listTypeOfAuto = this.linkTypeOfAuto.getAllTypeOfAutos();
			session.put("lstTypeOfAuto", this.listTypeOfAuto);
		} catch (Exception e) {
			addActionError("Loi:" + e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	public String deleteTypeOfAutos() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			linkTypeOfAuto.deleteTypeOfAuto(request.getParameter("id"));

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String preEditTypeOfAutos() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
		.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			if (null != typeOfAuto) {
				System.out.println("goi ham add");				
				String idTypeOfAuto = request.getParameter("id");
				System.out.println("ID:" + idTypeOfAuto);
				this.typeOfAuto = this.linkTypeOfAuto
						.getTypeOfAuto(idTypeOfAuto);
				System.out.println(typeOfAuto);
				TypeOfAuto ntyOfAuto = typeOfAuto;
				linkTypeOfAuto.saveAndUpdateTypeOfAutos(ntyOfAuto);
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setListTypeOfAuto(List<TypeOfAuto> listTypeOfAuto) {
		this.listTypeOfAuto = listTypeOfAuto;
	}

	public List<TypeOfAuto> getListTypeOfAuto() {
		return listTypeOfAuto;
	}

	public void setTypeOfAuto(TypeOfAuto typeOfAuto) {
		this.typeOfAuto = typeOfAuto;
	}

	public TypeOfAuto getTypeOfAuto() {
		return typeOfAuto;
	}

}
